/**
 * Created by 骆鹏 on 2017/7/12.
 */
var flowId = $.GetRequest().flowId
var flowName="";
var dataType=1;
var  layer,form,table,treeSelect;

function replaceReg(strs){
    var str=strs;
    var strsto=str.replace(/attend_leave/,qingjia).replace(/budgeting_process/g,yusuan).replace(/attend_evection/g,chucai).replace(/attend_out/g,waichu).replace(/document/g,gongjiao)
    return strsto;
}
function deletes(id,me) {
    $.layerConfirm({title:queding,content:qued,icon:0},function(){
        $.post('/flowModelAssociationSet/deleteFlowModelAssociationSet',{'id':id},function (json) {
            if(json.flag){
                $.layerMsg({content:delc,icon:1},function(){
                    $(me).parent().parent().remove()
                });
            }
        },'json')
    })

}

$(function () {
    layui.use(['layer', 'form','table','treeSelect'], function() {
        layer = layui.layer
            , form = layui.form
            , table = layui.table
            , treeSelect = layui.treeSelect;
        $.get('/flowModelAssociationSet/queryFlowModelAssociationSet',{'flowId':flowId},function (json) {
            if(json.flag){
                var arr=json.object.flow;
                var card=json.object.card;
                var str='';
                for(var i=0;i<arr.length;i++){
                    str+='<tr id="'+arr[i].flowRelId+'" status="'+arr[i].relationStatus+'" flowRange="'+arr[i].flowRange+'" timeRange="'+arr[i].timeRange+'"><td>'+arr[i].flowRefNo+'</td>\
                    <td>'+arr[i].flowRelName+'</td>'+
                        '<td style="width:10px">'+
                        '<input type="hidden" value="'+arr[i].dataDirection+'">'+
                        '</td>'+
                        '<td>'+arr[i].relationFlowName+'<input type="hidden" value="'+arr[i].relationFlowId+'"></td>\
                    <td title="'+arr[i].mappingDesc+'" type="'+arr[i].mapping+'">'+arr[i].mappingDesc+'</td>\
                    <td>'+arr[i].flowRelDesc+'</td>\
                        <td>\
                            <a href="javascript:void (0)" class="newsBtntwo" data-num="1">'+edit1+'</a>\
                            <a href="javascript:void (0)" onclick="deletes('+arr[i].flowRelId+',this)">'+del+'</a>\
                    </td></tr>';
                }
                $('.list').html(str);

                //卡片

                if(card.length>0){
                    let card0 = card[0];

                    //回显复选框
                    let relationFlowId = card0.relationFlowId.split(",");
                    for (let i = 0; i < relationFlowId.length ; i++) {
                        $('div[name="cardWrap"]').find('div[datatype="'+relationFlowId[i]+'"]').find('input[type="checkbox"]').attr('checked','checked');
                    }

                    //回显 选中的下拉框
                    let mapping = card0.mapping.split('|');
                    let mappingDesc = card0.mappingDesc.split('|');
                    for (let i = 0; i < mapping.length ; i++) {
                        let type = mapping[i].split('-');
                        $('div[name="cardWrap"]').find('div[datatype="'+type[0]+'"]').attr('lay-value',type[1]);

                        let typeName = mappingDesc[i].split('-');
                        $('div[name="cardWrap"]').find('div[datatype="'+typeName[0]+'"]').attr('lay-name',typeName[1]);
                    }

                    //回显制度
                    let str='';
                    let institutionContents=card0.institutionContents;
                    for (let i = 0; i < institutionContents.length; i++) {
                        //回显内容
                        if (institutionContents[i].instType==1){
                            institutionContents[i].instType='网页形式';
                        }else{
                            institutionContents[i].instType='文件形式';
                        }

                        str+='<li>' + institutionContents[i].instType+ ' | ' + institutionContents[i].instName + '</li>';
                    }
                    $('ul[name="details4"]').html(str);

                }

            }
        },'json');
    });
});


$(document).delegate('.newsBtntwo','click',function () {
    var stris='';
    var obj={};
    var _this = $(this);
    if($(this).attr('data-num')==0){
        stris='新建流程'
        obj.orderId=''
        obj.name=''
        obj.hmodule=''
        obj.flowId=''
        obj.dataDirection=''
        obj.hdesc=''
        obj.url='/flowModelAssociationSet/newFlowModelAssociationSet'
        obj.str=new1
        obj.disabled = true
        obj.status = 1
        obj.id = ""
        obj.timeRange=""
        obj.flowRange=""
        dataType = 1
    }else {
        stris='编辑流程'
        obj.orderId=$(this).parent().parent().find('td').eq(0).text()
        obj.name=$(this).parents('tr').find('td').eq(1).text()
        obj.hmodule=$(this).parent().parent().find('td').eq(1).find('input').val()
        obj.flowId=$(this).parent().parent().find('td').eq(3).find('input').val()
        obj.dataDirection=$(this).parent().parent().find('td').eq(2).find('input').val()
        // obj.dataDirections=$(this).parent().parent().find('td').eq(2).text()
        obj.mapping=$(this).parent().parent().find('td').eq(4).attr('type')
        obj.mappingDesc=$(this).parent().parent().find('td').eq(4).text()
        obj.hdesc=$(this).parent().parent().find('td').eq(5).text()
        obj.url='/flowModelAssociationSet/editFlowModelAssociationSet'
        obj.str='确定'
        obj.status = $(this).parents('tr').attr('status')
        obj.disabled = false
        obj.id = $(this).parents('tr').attr('id')
        obj.timeRange = $(this).parents('tr').attr('timeRange')
        obj.flowRange = $(this).parents('tr').attr('flowRange')
        dataType = obj.dataDirection
    }

    layer.open({
        title: stris,
        area: ['800px', '500px'],
        type:1,
        content: '<form id="saveRule" action="'+obj.url+'" class="layui-form"><div class="inputlayout">' +
            '<ul>' +
            '<li class="clearfix">' +
            '<label>排序号：</label><input type="text" name="flowRefNo" value="'+obj.orderId+'">' +
            '</li>' +
            '<li class="clearfix">' +
            '<label>关联名称 ：</label><input type="text" name="flowRelName" value="'+obj.name+'">' +
            '</li>' +
            '<li class="clearfix">' +
            '<label>关联状态 ：</label>' +
            ' <select name="relationStatus" data-value="'+obj.status+'"  disabled="'+obj.disabled+'"  lay-verify=""  lay-filter="status" >'+
            '<option value="1">开启</option>'+
            '<option value="0">关闭</option>'+
            '</select>   ' +
            '</li>' +
            '<li class="clearfix">' +
            '<label>关联范围：</label>' +
            ' <select name="flowRange"   data-value="'+obj.flowRange+'" lay-verify="" lay-filter="" >'+
            '<option value="">'+ds+'</option>'+
            '<option value="0">所有</option>'+
            '<option value="1">经办</option>'+
            '</select>   ' +
            '</li>' +
            '<li class="clearfix">' +
            '<label>时间范围：</label>' +
            ' <select name="timeRange"   data-value="'+obj.timeRange+'" lay-verify="" lay-filter="" >'+
            '<option value="">'+ds+'</option>'+
            '<option value="1">上月</option>'+
            '<option value="2">上季度</option>'+
            '<option value="3">上年度</option>'+
            '<option value="4">本月</option>'+
            '<option value="5">本季度</option>'+
            '<option value="6">本年</option>'+
            '</select>   ' +
            '</li>' +
            '<li class="clearfix">' +
            '<label>关联流程 ：</label>' +
            ' <select name="relationFlowId" data-value="'+obj.flowId+'"  lay-verify=""  lay-filter="process" >'+
            '<option value="">'+ds+'</option>'+
            '</select>   ' +
            '</li>' +
            '<li class="clearfix" style="position: relative;padding-bottom: 50px">' +
            '<label>'+dataY+' ：</label>' +
            '<ul class="business allacti"  ></ul>' +
            '<ul class="processS allacti" ></ul>' +
            '<button class="btnSave" type="button" style="position: absolute;left: 50%;margin-left: -80px;bottom: 7px;color:#fff;' +
            'background-color: #ccc;padding: 5px 10px;border-radius: 20px;cursor: pointer">'+palse+'</button>' +
            '</li>' +
            '<li class="clearfix">' +
            '<label>数据关系映射 ：</label>' +
            '<ol>' +
            '</ol>' +
            '</li>' +
            '<li class="clearfix">' +
            '<label>描述 ：</label><textarea name="flowRelDesc" value="'+obj.hdesc+'">'+obj.hdesc+'</textarea>' +
            '</li>' +
            '</ul>' +
            '</div><input type="hidden" name="relationType" value="2"><input type="hidden" name="mapping"><input type="hidden" name="mappingDesc"><input type="hidden" name="flowRelId" value="'+obj.id+'"></form>',
        btn: [obj.str, retu],
        yes: function (index) {
            var arrayObj=$('[name="maps"]');
            var str=''
            var strFomat=""
            for(var i=0;i<arrayObj.length;i++){
                strFomat += $(arrayObj[i]).prev().text()+'|';
                str+=$(arrayObj[i]).val()+'|'
            }

            $('[name="mapping"]').val(str);
            $('[name="mappingDesc"]').val(strFomat);
            $('[name="relationStatus"]').removeAttr('disabled')

            $('#saveRule').ajaxSubmit({
                type:'post',
                dataType:'json',
                data:{flowId:flowId},
                success:function (json) {
                    if(json.flag){
                        $.layerMsg({content:save,icon:1},function () {
                            window.location.reload();
                            layer.close(index)
                        })
                    }else {
                        $.layerMsg({content:json.msg,icon:2})
                        layer.close(index);
                    }
                }
            })
        },
        success:function () {
            $('[name="relationStatus"]').val(obj.status)
            form.render()
            var hmoduletwo=$('[name="hmodule"]').attr('data-value');//业务模块回显
            var business;//业务
            var str='';
            var processData;//流程

            if(_this.attr('data-num')==0){
                $('[name="relationStatus"]').attr('disabled','disabled')
            }else{
                $('[name="relationStatus"]').removeAttr('disabled')
            }
            $.get('/flowSort/getFlowTypes',function (json) {
                if(json.flag){
                    var arrobj=json.obj;
                    for(var i=0;i<arrobj.length;i++){
                        str+='<optgroup label="'+arrobj[i].sortName+'">' +
                            ''+function () {
                                var stroption='';
                                for(var m=0;m<arrobj[i].flowTypeModels.length;m++){
                                    stroption+='<option value="'+arrobj[i].flowTypeModels[m].flowId+'">' +
                                        ''+arrobj[i].flowTypeModels[m].flowName+'</option>'
                                }
                                return stroption;
                            }()+'</optgroup>'
                    }
                    $('[name="relationFlowId"]').append(str)
                    form.render()
                    $('[name="relationFlowId"]').next().find('dd').each(function (i,n) {
                        if($(this).attr('lay-value')==$('[name="relationFlowId"]').attr('data-value')){
                            $(this).click();
                            return false;
                        }
                    });
                }
            },'json')
            $('[name="dataDirection"]').next().find('dd').each(function (i,n) {
                if($(this).attr('lay-value')==$('[name="dataDirection"]').attr('data-value')){
                    $(this).click();
                    return false;
                }
            })
            $('[name="timeRange"]').next().find('dd').each(function (i,n) {
                if($(this).attr('lay-value')==$('[name="timeRange"]').attr('data-value')){
                    $(this).click();
                    return false;
                }
            })
            $('[name="flowRange"]').next().find('dd').each(function (i,n) {
                if($(this).attr('lay-value')==$('[name="flowRange"]').attr('data-value')){
                    $(this).click();
                    return false;
                }
            })
            $('[name="relationStatus"]').next().find('dd').each(function (i,n) {
                if($(this).attr('lay-value')==$('[name="relationStatus"]').attr('data-value')){
                    $(this).click();
                    return false;
                }
            })

            if(obj.mapping!=undefined) {
                var arraythr = obj.mapping.split('|');
                var arraythrs = obj.mappingDesc.split('|');
                var strthree = '';
                for (var q = 0; q < arraythr.length; q++) {
                    if (q != arraythr.length - 1) {
                        strthree += '<li><span>' + arraythrs[q] +  '</span><input type="hidden" name="maps" value="' + arraythr[q] + '"><b>x</b></li>'
                    } else if (arraythr.length == 1) {
                        strthree += '<li><span>' + arraythrs[q] + '</span><input type="hidden" name="maps" value="' + arraythr[q] + '"><b>x</b></li>'
                    }
                }
                $('.inputlayout ol').html(strthree)
            }

            //获取流程名称
            $.ajax({
                url:'/reportSettings/querybyFlowIdField',
                dataType:'json',
                type:'get',
                data:{'flowId':flowId},
                success:function(res){
                    var data = res.obj[0].printModel;
                    var str='';
                    var obj=[];

                    var len = $(data).find('.form_item').length
                    for(var i = 0;i<len;i++){
                        str+='<li>'+$(data).find('.form_item').eq(i).attr('title')+'<input type="hidden" value="'+$(data).find('.form_item').eq(i).attr('id')+'"></li>'
                        obj.push($(data).find('.form_item').eq(i))
                    }
                    business = obj;
                    $('.business').html(str);
                    flowName = res.obj[0].formName
                }
            })
            form.on('select(orientation)',function(data){
                dataType = data.value;
            })


            form.on('select(process)',function (data) {//流程
                $.post('/flowhook/queryFormId',{'folwId':data.value},function (json) {
                    if(json.flag){
                        workForm.init({
                                formhtmlurl: '../../form/formType',
                                resdata: {
                                    fromId: json.object.formId
                                },
                                flag: 3
                            },
                            function (datas) {
                                processData = datas;
                                var liStrs='';//流程
                                for(var m=0;m<processData.length;m++){
                                    liStrs+='<li>'+processData[m].title+'<input type="hidden" value="'+processData[m].name+'"></li>'
                                }
                                form.render();
                                $('.processS').html(liStrs)
                            });
                    }
                },'json')
            })

            $('[name="hmodule"]').next().find('dd').each(function (i,n) {
                if($(this).attr('lay-value')==hmoduletwo){
                    $(this).click();
                    return false;
                }
            })

            $(document).delegate('.allacti>li','click',function () {
                $(this).parent().find('li').removeClass('active')
                $(this).addClass('active')
            })

            $('.btnSave').click(function () {

                var textBusiness=$('.business .active').text()
                var inVBusiness=$('.business .active').find('input').val()
                var textprocessS=$('.processS .active').text()
                var inprocessS=$('.processS .active').find('input').val()
                if(textBusiness==''||textprocessS==''){
                    alert(qingFrist)
                    return false
                }
                var arrAus=$('.inputlayout ol li');
                var trueNum=null;
                for(var i=0;i<arrAus.length;i++){
                    if($(arrAus[i]).find('input').val()==(inVBusiness+'=>'+textprocessS)||$(arrAus[i]).find('input').val()==(textprocessS+'=>'+inVBusiness)){
                        alert(hhh)
                        trueNum=1;
                        return false
                    }
                }
                if(trueNum==1){
                    return
                }
                var str= '<li><span>' + $('.business .active').text() + '-' + $('.processS .active').text() + '</span><input type="hidden" name="maps" value="' + ($('.business .active').find('input').val()+'=>'+$('.processS .active').find('input').val()) + '"><b>x</b></li>'
                $(this).parent().next().find('ol').append(str)
            })

            $(document).delegate('.inputlayout ol li b','click',function () {
                $(this).parent().remove()
            })
        }
    })
})

$(document).delegate('.choose','click',function () {
    var stris='';
    var obj={};
    var _this = $(this);
    if($(this).attr('data-num')==0){
        stris='新建模块'
        obj.orderId=''
        obj.name=''
        obj.hmodule=''
        obj.flowId=''
        obj.dataDirection=''
        obj.hdesc=''
        obj.str=new1
        obj.disabled = true
        obj.status = 1
        obj.id = ""
        dataType = 1
    }else {
        stris='编辑模块'
        obj.orderId=$(this).parent().parent().find('td').eq(0).text()
        obj.name=$(this).parents('tr').find('td').eq(1).text()
        obj.hmodule=$(this).parent().parent().find('td').eq(1).find('input').val()
        obj.flowId=$(this).parent().parent().find('td').eq(3).find('input').val()
        obj.dataDirection=$(this).parent().parent().find('td').eq(2).find('input').val()
        obj.dataDirections=$(this).parent().parent().find('td').eq(2).text()
        obj.mapping=$(this).parent().parent().find('td').eq(4).text()
        obj.hdesc=$(this).parent().parent().find('td').eq(5).text()
        obj.url='/flowModelAssociationSet/editFlowModelAssociationSet'
        obj.str='确定'
        obj.status = $(this).parents('tr').attr('status')
        obj.disabled = false
        obj.id = $(this).parents('tr').attr('id')
        dataType = obj.dataDirection
    }

    layer.open({
        title: stris,
        area: ['800px', '500px'],
        type:1,
        content: '<form id="saveRule" action="'+obj.url+'" class="layui-form"><div class="inputlayout">' +
            '<ul>' +
            '<li class="clearfix">' +
            '<label>'+xuhao+' ：</label><input type="text" name="orderId" value="'+obj.orderId+'">' +
            '</li>' +
            '<li class="clearfix">' +
            '<label>'+mingcheng+' ：</label><input type="text" name="name" value="'+obj.name+'">' +
            '</li>' +
            '<li class="clearfix">' +
            '<label>状态 ：</label>' +
            ' <select name="status" data-value="'+obj.status+'"  disabled="'+obj.disabled+'"  lay-verify=""  lay-filter="status" >'+
            '<option value="1">开启</option>'+
            '<option value="0">关闭</option>'+
            '</select>   ' +
            '</li>' +
            '<li class="clearfix">' +
            '<label>'+yewu+' ：</label>' +
            ' <select name="module"  data-value="'+obj.hmodule+'"  lay-verify="" lay-filter="automaticCandidateTwo">'+
            '<option value="">'+ds+'</option>'+
            '</select>   ' +
            '</li>' +
            '<li class="clearfix" style="position: relative;padding-bottom: 50px">' +
            '<label>'+dataY+' ：</label>' +
            '<ul class="business allacti"  ></ul>' +
            '<ul class="processS allacti" ></ul>' +
            '<button class="btnSave" type="button" style="position: absolute;left: 50%;margin-left: -80px;bottom: 7px;color:#fff;' +
            'background-color: #ccc;padding: 5px 10px;border-radius: 20px;cursor: pointer">'+palse+'</button>' +
            '</li>' +
            '<li class="clearfix">' +
            '<label>'+guanxi+' ：</label>' +
            '<ol>' +
            '</ol>' +
            '</li>' +
            '<li class="clearfix">' +
            '<label>'+mao+' ：</label><textarea name="desc" value="'+obj.hdesc+'">'+obj.hdesc+'</textarea>' +
            '</li>' +
            '</ul>' +
            '</div><input type="hidden" name="mapping"><input type="hidden" name="plugin"><input type="hidden" name="mappingFormat"><input type="hidden" name="id" value="'+obj.id+'"></form>',
        btn: [obj.str, retu],
        yes: function (index) {
            var arrayObj=$('[name="maps"]');
            var str=''
            var strFomat = ""
            for(var i=0;i<arrayObj.length;i++){
                strFomat += $(arrayObj[i]).prev().text()+'|';
                str+=$(arrayObj[i]).val()+'|'
            }
            var text = $('[name="module"] option:selected').text()


            $('[name="mapping"]').val(str);
            $('[name="mappingFormat"]').val(strFomat);
            $('[name="plugin"]').val($('[name="module"]').val());

            $('[name="status"]').removeAttr('disabled')

            $('#saveRule').ajaxSubmit({
                type:'post',
                dataType:'json',
                data:{flowId:flowId,module:text},
                success:function (json) {
                    if(json.flag){
                        $.layerMsg({content:save,icon:1},function () {
                            window.location.reload();
                            layer.close(index)
                        })
                    }else {
                        $.layerMsg({content:json.msg,icon:2})
                    }
                }
            })
        },
        success:function () {
            $('[name="status"]').val(obj.status)
            form.render()
            var hmoduletwo=$('[name="module"]').attr('data-value');//业务模块回显
            var business;//业务
            var processData;//流程

            if(_this.attr('data-num')==0){
                $('[name="status"]').attr('disabled','disabled')
            }else{
                $('[name="status"]').removeAttr('disabled')
            }
            $.get('/code/getCode',{parentNo: 'FLOW_RELATION'},function(json){
                var data = json.obj;
                var str = '';
                for(var i=0;i<data.length;i++){
                    str += '<option  dataType="'+data[i].codeName+'"  value = "'+data[i].codeNo+'">'+data[i].codeName+'</option>'
                }
                $('[name="module"]').append(str)


                form.render();

            },'json')
            $('[name="dataDirection"]').next().find('dd').each(function (i,n) {
                if($(this).attr('lay-value')==$('[name="dataDirection"]').attr('data-value')){
                    $(this).click();
                    return false;
                }
            })

            if(obj.mapping!=undefined) {
                var arraythr = obj.mapping.split('|');
                var strthree = '';
                for (var q = 0; q < arraythr.length; q++) {
                    if (q != arraythr.length - 1) {
                        strthree += '<li><span>' + arraythr[q] + '(' + obj.dataDirections + ')</span><input type="hidden" name="maps" value="' + arraythr[q] + '"><b>x</b></li>'
                    } else if (arraythr.length == 1) {
                        strthree += '<li><span>' + arraythr[q] + '(' + obj.dataDirections + ')</span><input type="hidden" name="maps" value="' + arraythr[q] + '"><b>x</b></li>'
                    }
                }
                $('.inputlayout ol').html(strthree)
            }

            //获取流程名称
            $.ajax({
                url:'/reportSettings/querybyFlowIdField',
                dataType:'json',
                type:'get',
                data:{'flowId':flowId},
                success:function(res){
                    var data = res.obj[0].printModel;
                    var str='';
                    var obj=[];

                    var len = $(data).find('.form_item').length
                    for(var i = 0;i<len;i++){
                        str+='<li>'+$(data).find('.form_item').eq(i).attr('title')+'<input type="hidden" value="'+$(data).find('.form_item').eq(i).attr('id')+'"></li>'
                        obj.push($(data).find('.form_item').eq(i))
                    }
                    business = obj;
                    if(dataType == 1){
                        $('.business').html(str)
                    }else{
                        $('.processS').html(str)
                    }
                    flowName = res.obj[0].formName
                }
            })


            form.on('select(orientation)',function (data) {//数据方向
                if(business==undefined||processData==undefined){
                    return
                }

                $('.inputlayout ol').html('')
                var liStr='';//业务
                for(var i=0;i<business.length;i++){
                    liStr+='<li>'+business[i].attr("title")+'<input type="hidden" value="'+business[i].attr('name')+'"></li>'
                }
                var liStrs='';//流程
                for(var m=0;m<processData.length;m++){
                    liStrs+='<li>'+processData[m].comment+'<input type="hidden" value="'+processData[m].columnName+'"></li>'
                }
                if(data.value==1){
                    $('.business').html(liStr)
                    $('.processS').html(liStrs)
                    dataType=1
                }else if(data.value==2) {
                    $('.business').html(liStrs)
                    $('.processS').html(liStr)
                    dataType=2
                }

            })

            form.on('select(automaticCandidateTwo)',function (data) {//业务
                var con = $('[name="module"] option:selected').text()

                $.post('/flowModelAssociationSet/queryModelAssociationField',{'tableName':data.value},function (json) {
                    if(json.flag){
                        processData=json.obj;
                        var liStr='';//业务
                        for(var i=0;i<processData.length;i++){
                            liStr+='<li>'+processData[i].comment+'<input type="hidden" value="'+processData[i].columnName+'"></li>'
                        }
                        var strOption = '<option value="1">'+flowName+'-'+con+'</option><option value="2">'+con+'-'+flowName+'</option>'
                        $('[name="dataDirection"]').html(strOption)

                        $('[name="dataDirection"]').val(dataType)
                        form.render();

                        if($('[name="dataDirection"]').val()==1){
                            $('.processS').html(liStr)
                        }else if($('[name="dataDirection"]').val()==2){
                            $('.business').html(liStr)
                        }


                    }
                },'json')
            })

            $('[name="module"]').next().find('dd').each(function (i,n) {
                if($(this).attr('lay-value')==hmoduletwo){
                    $(this).click();
                    return false;
                }
            })

            $(document).delegate('.allacti>li','click',function () {
                $(this).parent().find('li').removeClass('active')
                $(this).addClass('active')
            })

            $('.btnSave').click(function () {

                var textBusiness=$('.business .active').text()
                var inVBusiness=$('.business .active').find('input').val()
                var textprocessS=$('.processS .active').text()
                var inprocessS=$('.processS .active').find('input').val()
                if(textBusiness==''||textprocessS==''){
                    alert(qingFrist)
                    return false
                }
                var arrAus=$('.inputlayout ol li');
                var trueNum=null;
                for(var i=0;i<arrAus.length;i++){
                    if($(arrAus[i]).find('input').val()==(inVBusiness+'=>'+textprocessS)||$(arrAus[i]).find('input').val()==(textprocessS+'=>'+inVBusiness)){
                        alert(hhh)
                        trueNum=1;
                        return false
                    }
                }
                if(trueNum==1){
                    return
                }
                var str='';
                $('[name="dataDirection"]').val(dataType)
                if($('[name="dataDirection"]').val()==1) {
                    str = '<li><span>' + $('.business .active').text() + '-' + $('.processS .active').text() + '('+$('[name="dataDirection"] option:selected').text()+')</span><input type="hidden" name="maps" value="' + ($('.business .active').find('input').val()+'=>'+$('.processS .active').find('input').val()) + '"><b>x</b></li>'
                }else if($('[name="dataDirection"]').val()==2){
                    str = '<li><span>' + $('.business .active').text() + '-' + $('.processS .active').text() + '('+$('[name="dataDirection"] option:selected').text()+')</span><input type="hidden" name="maps" value="' + ($('.business .active').find('input').val()+'=>'+$('.processS .active').find('input').val()) + '"><b>x</b></li>'
                }
                $(this).parent().next().find('ol').append(str)
            })

            $(document).delegate('.inputlayout ol li b','click',function () {
                $(this).parent().remove()
            })

        }
    })
})

$('.news span').click(function () {
    $(this).parent().siblings().removeClass('active')
    $(this).parent().addClass('active')
    if($(this).attr('data-num')==2){
        $.layerMsg({content:process})
    }
})

//普通卡片的点击事件
function setting(type){
    var ralationIpt = $(this).parents('.card').find('.relationIpt').val();
    var names = "";
    if(type == 1 || type == 2){
        names = '姓名关联到表单字段'
    }else{
        names = '固定资产编号关联到表单字段'
    }
    layer.open({
        type:1,
        btn:['确定','关闭'],
        title:'设置关联表单字段',
        area:['550px','300px'],
        content:'<form id="sets"  class="layui-form">' +
            '<div class="inputlayout" style="height:180px">' +
            '<ul>' +
            '<li class="clearfix">' +
            '<label style="width: 190px;">'+names+':</label>' +
            '<select  name="relationZ"  lay-filter="status"  style="width: 90px;">' +
            '</select> ' +
            '</li>' +
            '</ul>' +
            '</div>' +
            '</form>',
        success:function(){
            form.render();
            $.post('/flowhook/queryFormId',{'folwId':flowId},function (json) {
                if(json.flag){
                    workForm.init({
                            formhtmlurl: '../../form/formType',
                            resdata: {
                                fromId: json.object.formId
                            },
                            flag: 3
                        },
                        function (datas) {
                            var processData = datas;
                            var liStrs='';//流程
                            for(var m=0;m<processData.length;m++){
                                liStrs+='<option value="'+processData[m].name+'" name="'+processData[m].title+'">'+processData[m].title+'</option>'
                            }
                            $('select[name="relationZ"]').html(liStrs);

                            //回显
                            var layvalue=$('div[dataType="'+type+'"]').attr('lay-value');
                            if(layvalue != ''){
                                $('select[name="relationZ"]').val(layvalue);
                            }
                            form.render();
                        });
                }
            },'json');
        },
        yes:function(index){
            var value=$('select[name="relationZ"]').val();
            $('div[dataType="'+type+'"]').attr('lay-value',value);

            var text=$('select[name="relationZ"]').find('option[value="'+value+'"]').text();
            $('div[dataType="'+type+'"]').attr('lay-name',text);
            layer.close(index);
        }
    })
}

//制度卡片设置的点击事件
function institution(type) {
    layer.open({
        type: 1,
        isOutAnim: true,
        maxmin: true,
        area: ['70%', '70%'],
        title: '选择制度',
        btnAlign: 'c',
        btn: ['保存', '关闭'],
        content: '<div >\n' +
            '<form class="layui-form" style=" margin-top: 25px; ">' +
            '<div class="layui-form-item">\n' +
            '    <div class="layui-inline">\n' +
            '      <label class="layui-form-label">制度类型</label>\n' +
            '      <div class="layui-input-inline">\n' +
            '        <select name="instType" lay-filter="aihao">' +
            '             <option value="2">文件类型</option>  ' +
            '             <option value="1">网页类型</option>  ' +
            '        </select>\n' +
            '      </div>\n' +
            '    </div>\n' +
            '    <div class="layui-inline">\n' +
            '      <label class="layui-form-label">制度分类</label>\n' +
            '      <div class="layui-input-inline">\n' +
            '        <input type="text" id="sortId" name="sortId" autocomplete="off" class="layui-input">\n' +
            '      </div>\n' +
            '    </div>\n' +
            '    <div class="layui-inline">\n' +
            '      <label class="layui-form-label">制度名称</label>\n' +
            '      <div class="layui-input-inline">\n' +
            '        <input type="text" id="instName" name="instName" placeholder="请输制度名称或关键字" autocomplete="off" class="layui-input">\n' +
            '      </div>\n' +
            '    </div>\n' +
            '    <div class="layui-inline">\n' +
            '      <label class="layui-form-label">' +
            '          <button type="button" lay-submit class="layui-btn layui-btn-sm" lay-filter="insSearch" style="width: 100%;">搜索</button>' +
            '      </label>\n' +
            '    </div>\n' +
            '  </div>' +
            '</form>' +
            '    <table id="demo" lay-filter="test"></table>\n' +
            '</div>',
        success: function(layero, index) {
            //渲染下拉树
            treeSelect.render( {
                elem: '#sortId',     // 选择器
                data: '/InstitutionSort/findSortDataTree',  // 数据
                type: 'get',      // 异步加载方式：get/post，默认get
                placeholder: '请选择',   // 占位符
                search: true,  // 是否开启搜索功能：true/false，默认false
                style: {   // 一些可定制的样式
                    folder: {enable: true},
                    line: {enable: true}
                },
                click: function(d){     // 点击回调
                    $('#sortId').val(d.current.id);
                    form.render();
                },
                // 加载完成后的回调函数
                success: function (d) {
                    form.render();
                }
            });

            //渲染表格
            table.render({
                elem:'#demo',
                toolbar:false,
                page:false,
                url:'/InstitutionContent/findContentWhere',
                where:{
                    approveStatus:'1',
                    instStatus:'1',
                    instType:'2',
                },
                parseData:function (res){
                    return {
                        code: 0,
                        msg: res.msg,
                        count: res.totleNum,
                        data: res.obj
                    }
                },
                cols:[[
                    {type:'checkbox',align:'center'}
                    ,{field:'instType',title:'类型',align:'center',templet:function (d) {
                            if (d.instType=='1'){
                                return '网页形式';
                            }else if(d.instType=='2'){
                                return '文档形式';
                            }
                        }}
                    ,{field:'instName',title:'文档',align:'center',
                        templet:function (d) {
                            return '<a href="javascript:;" style="text-decoration:underline;color:blue">'+d.instName+'</a>'
                        },event:'title'

                    }
                    ,{field:'instNumber',title:'制度编号',align:'center'}
                    ,{field:'fileNum',title:'附件数量',align:'center'}
                    ,{field:'instStatus',title:'使用状态',align:'center',templet:function (d) {
                            if (d.instStatus==1){
                                return "生效"
                            }else{
                                return "停用"
                            }
                        }}
                    ,{field:'approveStatus',title:'审核状态',align:'center',templet:function (d) {
                            if (d.approveStatus==0){
                                return "待批"
                            }else if (d.approveStatus==1){
                                return "已批"
                            }else{
                                return "未批"
                            }
                        }},
                    {field:'version',title:'当前版本',align:'center'}
                ]]
            });

            //监听表单提交
            form.on('submit(insSearch)', function(data){
                var obj = data.field;

                //审批状态(0-待批,1-已准,2-未准，3-未提交)
                obj['approveStatus'] = '1';
                obj['keyWords'] = obj['instName'];

                //重载表格
                table.reload('demo', {
                    where: obj
                });
                return false;
            });
        },
        yes: function(index, layero) {
            var checkStatus = table.checkStatus('demo');
            var data=checkStatus.data;
            let instIds='';
            let instNames='';
            let str='';
            for (let i=0;i<data.length;i++){
                //赋值ID和名称
                instIds+=data[i].instId+',';
                instNames+=data[i].instName+',';

                //回显内容
                if (data[i].instType==1){
                    data[i].instType='网页形式';
                }else{
                    data[i].instType='文件形式';
                }

                str+='<li>' + data[i].instType+ ' | ' + data[i].instName + '</li>';
            }
            $('ul[name="details4"]').html(str);

            //赋值名称和ID
            $('div[dataType="'+type+'"]').attr('lay-value',instIds);
            $('div[dataType="'+type+'"]').attr('lay-name',instNames);

            layer.close(index);
        }
    });
}

//卡片保存事件
$(document).delegate('a[name="cardBtn"]','click',function () {

    var relationFlowId=''; //关联字段
    var mapping=''; //数据关系映射
    var mappingDesc=''; //中文数据关联映射
    //获取全部标签
    $('div[name="cardWrap"] div[class="card"]').each(function (index,item) {

        let isChecked = $(item).find('input[type="checkbox"]').is(":checked");
        if (isChecked){
            let dataType = $(item).attr('dataType');
            let lay_value = $(item).attr('lay-value');
            let lay_name = $(item).attr('lay-name');
            relationFlowId+=dataType+',';
            mapping+=dataType+'-'+lay_value+'|';
            mappingDesc+=dataType+'-'+lay_name+'|';
        }
    });

    //获取每个卡片的值
    $.post('/flowModelAssociationSet/newFlowModelAssociationSet',{
        flowId:flowId,
        relationType:'1',
        relationFlowId:relationFlowId,
        mapping:mapping,
        mappingDesc:mappingDesc
    },function(res){
        if (res.flag){
            window.location.reload();
        }
    });
});